package com.unicom.office.config;

import com.unicom.office.func.FuncBase;
import com.unicom.office.templatefunc.*;
import com.unicom.office.templateproc.ExcelFilepathProc;
import com.unicom.office.type.NumberTypeItem;
import com.unicom.office.type.StringTypeItem;
import com.unicom.office.type.TypeItem;
import com.unicom.office.var.VarItem;

import java.util.HashMap;
import java.util.Map;

public abstract class OfficeConfig {
    private Map<String, TemplateBase> templateFuncMap = new HashMap<String, TemplateBase>();
    private Map<String, VarItem> varItemMap = new HashMap<String, VarItem>();
    private Map<String, TypeItem> typeItemMap = new HashMap<String, TypeItem>();
    private Map<String, FuncBase> macroMap = new HashMap<String, FuncBase>();
    private MarkerConfig markerConfig = new MarkerConfig("$", "");

    {
        register(AddFunc.INSTANCE);
        register(JoinFunc.INSTANCE);
        register(MultFunc.INSTANCE);
        register(RoundFunc.INSTANCE);
        register(ExcelFilepathProc.INSTANCE);
        register(NumberTypeItem.INSTANCE);
        register(StringTypeItem.INSTANCE);
    }

    public OfficeConfig(){
    }

    public OfficeConfig(String configPath){
        parseConfigFile(configPath);
    }

    public abstract void parseConfigFile(String configPath);

    public TemplateBase getTemplateFunc(String name){
        if(!templateFuncMap.containsKey(name)) {
            throw new RuntimeException("不存在的模板方法" + name);
        }
        return templateFuncMap.get(name);
    }

    public VarItem getVarItem(String name){
        if(!varItemMap.containsKey(name)) {
            throw new RuntimeException("不存在的变量");
        }
        return varItemMap.get(name);
    }

    public Map<String, VarItem> getVarItemMap(){
        return this.varItemMap;
    }

    public TypeItem getTypeItem(String name){
        if(!typeItemMap.containsKey(name)) {
            throw new RuntimeException("不存在的类型");
        }
        return typeItemMap.get(name);
    }

    public OfficeConfig register(TemplateBase templateFunc) {
        templateFuncMap.put(templateFunc.getName(), templateFunc);
        return this;
    }

    public OfficeConfig register(VarItem varItem){
        varItemMap.put(varItem.getName(), varItem);
        return this;
    }

    public OfficeConfig register(TypeItem typeItem){
        typeItemMap.put(typeItem.getName(), typeItem);
        return this;
    }
}
